package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.util.Result;
import cn.ncu.wechat.vo.MemberLevelVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.MemberLevel;
import cn.ncu.wechat.service.MemberLevelService;
import cn.ncu.wechat.dao.MemberLevelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 
* @description 针对表【member_level(会员等级表)】的数据库操作Service实现
* @createDate 2025-01-07 16:10:24
*/
@Service
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel>
    implements MemberLevelService{
    @Autowired
    private MemberLevelMapper memberLevelMapper;

    public Result<?> getMemberList(){
        try{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("id","name","min_points","icon","discount");
            List<MemberLevel> memberLevels = memberLevelMapper.selectList(queryWrapper);
            List<Map<String, Object>> resultList = memberLevels.stream().map(memberLevel -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", memberLevel.getId());
                map.put("name", memberLevel.getName());
                map.put("minPoints", memberLevel.getMinPoints());
                map.put("icon", memberLevel.getIcon());
                map.put("discount", memberLevel.getDiscount());
                return map;
            }).collect(Collectors.toList());
            return Result.success(resultList);
        }catch (Exception e){
            return Result.error(500,"服务器异常"+e);
        }
    }

    public Result<?> updateMemberList(MemberLevel memberLevel){
        try {
            if (memberLevelMapper.insertOrUpdate(memberLevel)) return Result.success();
            return Result.error(500,"服务器错误");
        } catch (Exception e){
            return Result.error(500,"服务器错误");
        }
    }

    public Result<?> getMemberLevelsDetail(){
        try{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select("id","name","min_points","icon","discount","benefits","status","create_time");
            List<MemberLevel> memberLevelList = memberLevelMapper.selectList(queryWrapper);
            List<MemberLevelVo> memberLevelVos = new ArrayList<>();
            for (MemberLevel m:memberLevelList) {
                MemberLevelVo memberLevelVo = new MemberLevelVo();
                memberLevelVo.toVo(m);
                memberLevelVos.add(memberLevelVo);
            }
            return Result.success(memberLevelVos);
        }catch (Exception e){
            return Result.error(500,"服务器异常"+e);
        }
    }

    public Result<?> updateOrAddMemberLevels(MemberLevelVo memberLevelVo){
        try{
            MemberLevel memberLevel = memberLevelVo.VoToMember();
            if (memberLevelMapper.insertOrUpdate(memberLevel)){
                return Result.success("保存成功");
            }
            return Result.error(500,"服务器错误");
        }catch (Exception e){
            return Result.error(500,"服务器异常"+e);
        }
    }
}




